package cn.com.tarotframework.server.workflow.service.impl;

import cn.com.tarotframework.server.workflow.eu.FlowStatus;
import cn.com.tarotframework.server.workflow.po.FlowNode;
import cn.com.tarotframework.server.workflow.service.IThreadFlowNodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;


@Slf4j
@Service
public class ThreadFlowNodeStateServiceImpl implements IThreadFlowNodeService {


    /**
     * 执行成功，由：EXECUTE_WAIT状态，更改为：EXECUTE_SUCCESS状态
     * @param flowNode 节点信息
     * @return FlowNode
     */
    @Override
    public FlowNode executeComplete(FlowNode flowNode) {
        flowNode.setStatus(FlowStatus.EXECUTE_SUCCESS);
        log.info("线程池名称：" + Thread.currentThread().getThreadGroup().getName() + "， 线程名称：" + Thread.currentThread().getName() + ", 节点信息：" + flowNode);
        return flowNode;
    }

    /**
     * 执行成功，由：EXECUTE_WAIT状态，更改为：EXECUTE_COMPLETE状态
     * @param flowNodes 节点信息集合
     * @return FlowNode
     */
    @Override
    public List<FlowNode> executeComplete(List<FlowNode> flowNodes) {
         flowNodes.forEach(this::executeComplete);
         return flowNodes;
    }

    /**
     * 执行失败，由：EXECUTE_WAIT状态，更改为：EXECUTE_ERROR状态
     * @param flowNode 节点信息
     * @return FlowNode
     */

    @Override
    public CompletableFuture<FlowNode> executeFail(FlowNode flowNode) {
        flowNode.setStatus(FlowStatus.EXECUTE_ERROR);
        log.info("线程池名称：" + Thread.currentThread().getThreadGroup().getName() + "， 线程名称：" + Thread.currentThread().getName() + ", 节点信息：" + flowNode);
        return CompletableFuture.completedFuture(flowNode);

    }

    /**
     * 未执行，由：EXECUTE_WAIT状态，更改为：EXECUTE_NOT状态
     * @param flowNode 节点信息
     * @return FlowNode
     */
    @Override
    public FlowNode executeStop(FlowNode flowNode) {
        flowNode.setStatus(FlowStatus.EXECUTE_NOT);
        log.info("线程池名称：" + Thread.currentThread().getThreadGroup().getName() + "， 线程名称：" + Thread.currentThread().getName() + ", 节点信息：" + flowNode);
        return flowNode;
    }

}
